package com.it.sun.redismq.controller;

import cn.hutool.json.JSONUtil;
import com.it.sun.redismq.vo.Message;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName com.it.sun.redismq.controller.IndexController
 * @Author sun
 * @Date 2021/1/28 10:38
 * @Version V1.0
 * Description  <请输入一句话描述该类功能>
 * Copyright (C) XXXX  since 2021/1/28
 */
@RestController
public class IndexController {

    private RedisTemplate redisTemplate;


    public IndexController(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @GetMapping("/topic/{key}/")
    @ApiOperation("主题订阅")
    public void topic(@PathVariable String key, Message<String> message) {
        redisTemplate.opsForList ().leftPush (key, JSONUtil.toJsonStr (message));

    }

    @GetMapping("/pubsub/{key}")
    @ApiOperation("广播消息")
    public void pubsub(@PathVariable String key, Message<String> message) {
        redisTemplate.convertAndSend (key, JSONUtil.toJsonStr (message));
    }
}



